<?php

/**
 * 客栈老板订单管理
 */
class bookAction extends globalAction {
    private $orderDao, $orderViewDao;

    public function _initialize() {
		parent::_initialize();
        parent::_loginRequire();
        $this->orderDao = D('Order');
        $this->orderViewDao = $this->orderDao->switchModel("View",array("viewFields"));
	}
    public function index() {
        /***********************************************************************
         * 构建筛选条件
         **********************************************************************/
        //属于客栈老板其下的客栈
        $innDao = D('Inn');
        $myInnSubQuery = $innDao
            ->field($innDao->_map['id'])
            ->where(array($innDao->_map['uid']=>__USERID__))
            ->buildSql();
        $conditions[$this->orderDao->_map['innId']] = array('exp', 'in '.$myInnSubQuery);
        //没有被删除
        $conditions[$this->orderDao->_map['isDel']] = array('exp', 'is null');
        //订单号以H1开头的才是客栈订单
        $conditions[$this->orderDao->_map['orderNum']] = array('like', 'h1%');

        //用户搜索项目
        if(!empty($_REQUEST['contact']))
            $conditions[$this->orderDao->_map['contact']] = array('like', "%{$_REQUEST['contact']}%");
        if(!empty($_REQUEST['mobile']))
            $conditions[$this->orderDao->_map['textContent']] = array('like', "%{$_REQUEST['mobile']}%");
        if(!empty($_REQUEST['email']))
            $conditions[$this->orderDao->_map['email']] = array('like', "%{$_REQUEST['email']}%");
        if(!empty($_REQUEST['orderNum']))
            $conditions[$this->orderDao->_map['orderNum']] = array('like', "%{$_REQUEST['orderNum']}%");
        if($_REQUEST['selectType']=='uncheckin') {
            $conditions[$this->orderDao->_map['checkinDate']] = array('EGT', strtotime(date('Y-m-d')));
            $conditions[$this->orderDao->_map['status']] = array('neq', 2);//2表示拒绝入住
            $order[$this->orderDao->_map['checkinDate']] = 'asc';
        } else {
            $order[$this->orderDao->_map['orderTime']] = 'desc';
        }

        import('ORG.Util.Page');
        $count = $this->orderDao->where($conditions)->count();
        $Page  = new Page($count,10);
        $pageBar  = $Page->show();
        $this->assign('pageBar', $pageBar);

        $orderList = $this->orderViewDao
            ->where($conditions)
            ->order($order)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        $this->assign('orderList', $orderList);

        C('TOKEN_ON',false);
        $this->display();
    }

    public function details() {
        $orderId = intval($_GET['id']);
        $orderDetails = $this->getOrder($orderId);
        $this->assign('orderDetails', $orderDetails);
        $this->display();
    }

    public function accept() {
        $orderId = intval($_GET['id']);
        $orderDetails = $this->getOrder($orderId);
        if($orderDetails['orderType'] == '即')
            parent::_message(
                'error',
                '即时预定类型的订单无需人工处理！',
                C('TMPL_PARSE_STRING.__KZURL__').'/book/details-id-'.$orderDetails['id']
            );
        if(!empty($orderDetails['status']))
            parent::_message(
                'error',
                '此订单已处理过了！',
                C('TMPL_PARSE_STRING.__KZURL__').'/book/details-id-'.$orderDetails['id']
            );

        $this->orderDao->where(array(
            $this->orderDao->_map['id'] => $orderDetails['id']
        ))->setField(array(
            $this->orderDao->_map['status']=>'1',
            $this->orderDao->_map['dealTime']=>time()
        ));

        redirect(C('TMPL_PARSE_STRING.__KZURL__').'/book/details-id-'.$orderDetails['id']);
    }
    public function reject() {
        $orderId = intval($_GET['id']);
        $orderDetails = $this->getOrder($orderId);
        if($orderDetails['orderType'] == '即')
            parent::_message(
                'error',
                '即时预定类型的订单无需人工处理！',
                C('TMPL_PARSE_STRING.__KZURL__').'/book/details-id-'.$orderDetails['id']
            );
        if(!empty($orderDetails['status']))
            parent::_message(
                'error',
                '此订单已处理过了！',
                C('TMPL_PARSE_STRING.__KZURL__').'/book/details-id-'.$orderDetails['id']
            );

        $this->orderDao->where(array(
            $this->orderDao->_map['id'] => $orderDetails['id']
        ))->setField(array(
            $this->orderDao->_map['status']=>'2',
            $this->orderDao->_map['dealTime']=>time()
        ));

        redirect(C('TMPL_PARSE_STRING.__KZURL__').'/book/details-id-'.$orderDetails['id']);
    }

    private function getOrder($orderId) {
        if(empty($orderId)) {
            parent::_message('error','订单ID错误！');
        }

        //属于客栈老板其下的客栈
        $innDao = D('Inn');
        $myInnSubQuery = $innDao
            ->field($innDao->_map['id'])
            ->where(array($innDao->_map['uid']=>__USERID__))
            ->buildSql();
        //订单ID
        $conditions[$this->orderDao->_map['id']] = array('eq', $orderId);
        //订单所属客栈归掌柜所有
        $conditions[$this->orderDao->_map['innId']] = array('exp', 'in '.$myInnSubQuery);
        //没有被删除
        $conditions[$this->orderDao->_map['isDel']] = array('exp', 'is null');
        //订单号以H1开头的才是客栈订单
        $conditions[$this->orderDao->_map['orderNum']] = array('like', 'h1%');

        $orderDetails = $this->orderViewDao
            ->where($conditions)
            ->find();

        if(empty($orderDetails)) {
            parent::_message('error','没有找到此订单！');
        }

        return $orderDetails;
    }
}